﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace WebberCross.JavaScriptBridge.WebParts.JSBridgeDemoWebPart
{
    [ToolboxItemAttribute(true)]
    public class JSBridgeDemoWebPart : SilverlightWebPartBase
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/WebberCross.JavaScriptBridge.WebParts/JSBridgeDemoWebPart/JSBridgeDemoUserControl.ascx";

        string scriptPath = "/_layouts/WebberCross.JavaScriptBridge/";
        string cssPath = "/_layouts/STYLES/WebberCross.JavaScriptBridge/";

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            // Include the required javascript files
            base.RegisterScriptFile("silverlightError", scriptPath + "silverlightError.js");

            // These are needed for the JSBridge
            base.RegisterScriptFile("JSBridge", scriptPath + "JSBridge.js");
            base.RegisterScriptFile("jquery", scriptPath + "jquery-1.4.2.min.js");
        }

        protected override void CreateChildControls()
        {
            // CSS link
            base.RegisterStyleSheet(cssPath + "SilverlightWebPart.css");
            
            // Insert UserControl
            SilverlightUserControlBase control = Page.LoadControl(_ascxPath) as SilverlightUserControlBase;
            control.WebPart = this;
            Controls.Add(control);

            // This registers the Silverlight application for the JS Bridge
            base.RegisterJSBridge();
        }
    }
}
